﻿using LY.Manage.Model;
using LY.Manage.Model.SearchParam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace LY.Manage.WebApp.Controllers
{
    public class SysMenuController : BaseController
    {
        //
        // GET: /SysMenu/
        IBLL.Isys_menuService sys_menuService = new BLL.sys_menuService();
        public ActionResult Index()
        {
            return View();
        }

        public string GetName(string parentCode)
        {
            string result = "";
            var data = sys_menuService.LoadEntities(u => u.MenuCode==parentCode).FirstOrDefault();

            if (data == null)
                return result;
            result = data.MenuName;

            return result;
        }


        #region 获取列表数据 GetDataInfoList
        public ActionResult GetDataInfoList()
        {
            var data = sys_menuService.LoadEntities(u => true);
            var temp = from x in data.ToList()
                     select new
                     {
                         Id = x.Id,
                         MenuName = x.MenuName,
                         MenuCode = x.MenuCode,
                         MenuNameFu = GetName(x.ParentCode),
                         IconClass = x.IconClass,
                         IconURL = x.IconURL,
                         IsVisible = x.IsVisible,
                         IsEnable = x.IsEnable,
                         MenuSeq = x.MenuSeq

                     };

            return Json(temp);

        }
        #endregion

        #region 显示添加数据页面 ShowAddInfo
        public ActionResult ShowAddInfo()
        {
            ParentDropDownList();
            return View();
        }

        #endregion
        #region 添加数据方法 AddInfo

        public ActionResult AddInfo(sys_menu entity)
        {
            String objectId = Request["ObjectId"];

            if(entity.ParentCode==null)
            {
                entity.ParentCode = "0";
            }
            sys_menuService.AddEntity(entity);

            return Content("ok");

        }
        #endregion
        #region 显示编辑页面 ShowEditInfo
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);


            sys_menu model = sys_menuService.LoadEntities(u => u.Id == id).FirstOrDefault(); ;

            ParentDropDownList();
            return View(model);
        }
        #endregion
        #region 编辑数据方法 EditInfo
        public ActionResult EditInfo(sys_menu entity)
        {
            //1.根据主键ID查询数据
            sys_menu menu = sys_menuService.LoadEntities(u => u.Id == entity.Id).FirstOrDefault();
            if (entity == null)
            {
                return Content("no");
            }

            #region 2.设置要修改的数据
            //用户表应该修改什么字段
            menu.MenuName = entity.MenuName;
            menu.MenuCode = entity.MenuCode;
            menu.ParentCode=entity.ParentCode;
            menu.IconClass=entity.IconClass;
            menu.IconURL=entity.IconURL;
            menu.IsVisible = entity.IsVisible;
            menu.IsEnable = entity.IsEnable;
            menu.MenuSeq = entity.MenuSeq;
            menu.UpdateDate = DateTime.Now;
            menu.UpdatePerson = "管理员";
            #endregion
            //3.更新数据，并返回
            if (sys_menuService.UpdateEntity(menu))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }
        #endregion
        #region 删除数据方法 DeleteInfo
        public ActionResult DeleteInfo()
        {
            string strId = Request["strId"];
            int id = Convert.ToInt32(strId);
            sys_menu entity = sys_menuService.LoadEntities(u => u.Id == id).FirstOrDefault();

            if (entity == null)
            {
                return Content("no");
            }
            //将list集合存储的要删除的记录的编号传递到业务层
            if (sys_menuService.DeleteEntity(entity))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }
        }
        #endregion
        public void ParentDropDownList()
        {
            //1.1查询YzSeriesEntity的数据  
            List<Model.sys_menu> seriesList = sys_menuService.LoadEntities(u => u.ParentCode == "0").ToList();
            List<SelectListItem> selectList = new List<SelectListItem>();
            selectList.Add(new SelectListItem { Value = "0", Text = "请选择" });
            foreach (var plitem in seriesList)
            {
                string parentName = plitem.MenuName;
                string parentId = plitem.MenuCode.ToString();
               
                selectList.Add(new SelectListItem
                {
                    Text = "├-" + parentName,
                    Value = parentId
                });

                string  pid = plitem.MenuCode;
                string tag = "　|- ";
                ChildrenList(pid, tag, selectList);
            }
            ViewBag.DevNameList = new SelectList(selectList, "Value", "Text");



        }
        public void ChildrenList(string pid, string tag, List<SelectListItem> selectList)
        {
            StringBuilder strWhere = new StringBuilder();
            // var childrenList = bll.GetModelList(strWhere.ToString()).Where(d => d.ParentId == pid);
            var childrenList = sys_menuService.LoadEntities(u => u.ParentCode == pid).ToList();

            foreach (var clitem in childrenList)
            {
                string clname = tag + clitem.MenuName;
                string clid = clitem.MenuCode.ToString();
                selectList.Add(new SelectListItem
                {
                    Text = clname,
                    Value = clid
                });
                string pid2 = clitem.MenuCode;
                string tag2 = tag + " -- ";
                ChildrenList(pid2, tag2, selectList);
            }
        }

    }
}

